<template>
  <div class="login-container">
    <div class="form-box">
      <h2 class="form-title">用户登录</h2>
      <form @submit.prevent="loginUser">
        <div class="form-group">
          <label for="username" class="form-label">用户名</label>
          <input type="text" class="form-control" id="username" v-model="username" required />
        </div>
        <div class="form-group">
          <label for="password" class="form-label">密码</label>
          <input type="password" class="form-control" id="password" v-model="password" required />
        </div>
        <button type="submit" class="btn btn-primary">登录</button>
      </form>
      <p v-if="errorMessage" class="text-danger">{{ errorMessage }}</p>
    </div>
  </div>
</template>

<script>
export default {
  name: 'LoginPage',
  data() {
    return {
      username: '',
      password: '',
      errorMessage: '',
    };
  },
  methods: {
    async loginUser() {
      try {
        const response = await this.$axios.post('/user/login', {
          username: this.username,
          password: this.password,
        });
        // 保存 token 等操作，跳转到主页面
        if (response.status === 200) {
          localStorage.setItem('token', response.data.token);
          localStorage.setItem('role', response.data.role);
          localStorage.setItem('userId', response.data.userId);
        // 跳转到主页面
        if (response.data.isCompleted===1 && response.data.role === "VISITOR") {
          this.$router.push('/index');
        }else{
          this.$router.push('/questionnaire');
        }
        if(response.data.role === "CONSULTANT"){
          this.$router.push('/appointments');
        }

        }else{
          console.error(response)
        }
      } catch (error) {
        this.errorMessage = error.response ? error.response.data : '登录失败，请重试';
      }
    },
  },
};
</script>

<style scoped>
/* 背景渐变色 */
.login-container {
  background: linear-gradient(135deg, #87CEFA, #ffb6c1); /* 渐变背景色 */
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}

/* 登录表单容器 */
.form-box {
  background-color: white;
  border-radius: 12px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  padding: 30px;
  width: 100%;
  max-width: 450px;
}

/* 表单标题 */
.form-title {
  text-align: center;
  font-size: 1.8rem;
  color: #4e73df; /* 深蓝色 */
  margin-bottom: 20px;
}

/* 输入框和选择框样式 */
.form-group {
  margin-bottom: 20px;
  display: flex;
  align-items: center;
}

.form-group label {
  font-size: 1rem;
  font-weight: bold;
  margin-right: 15px; /* 给标签和输入框之间增加间距 */
  width: 100px; /* 标签的宽度固定，左对齐 */
}

.form-control {
  width: 100%; /* 输入框的宽度为100%，保证一致性 */
  padding: 12px 15px;
  border-radius: 8px;
  border: 1px solid #ddd;
  background-color: #f8f9fa;
  font-size: 1rem;
  transition: border-color 0.3s ease;
  box-sizing: border-box;
}

.form-control:focus {
  border-color: #4e73df;
  outline: none;
  box-shadow: 0 0 8px rgba(72, 123, 234, 0.5);
}

/* 按钮样式 */
button {
  background-color: #4e73df;
  color: white;
  padding: 12px;
  border-radius: 10px;
  border: none;
  width: 100%;
  cursor: pointer;
  font-size: 1.1rem;
  transition: background-color 0.3s ease;
}

button:hover {
  background-color: #2e59d9;
}

/* 错误消息 */
.text-danger {
  color: red;
  text-align: center;
  margin-top: 15px;
  font-size: 1rem;
}
</style>
